/*
    page_alloc.h

    contains page allocation control tables and function headers

    Author: Aidan Goddard 3/8/13
*/

#ifndef PAGE_ALLOC_H_INCLUDED
#define PAGE_ALLOC_H_INCLUDED

#include"kernel_tables.h"

typedef struct TBL_PG_ALLOC TBL_PG_ALLOC;
struct __attribute__((__packed__)) TBL_PG_ALLOC
{
    uint8_t volatile	pmap[512*1024];	    // page allocation map
    SCH_CLS				lock;				// global page allocation lock
    uint32_t volatile	page_max;			// the highest page number in the system + 1;
    uint32_t volatile	stack_index;		// index into allocation stack
    uint32_t volatile   free_pages;         // a rough guide to how many pages are available in the system
};

// page states
#define PAGE_DISABLED 0xff
#define PAGE_RESERVED 0xfe			// for future use
#define PAGE_MAPPED 0xfd			// marks that the page has already been mapped. can override PAGE_DISABLED (possibly for hardware I/O or system tables)
#define PAGE_FREE 0
#define PAGE_ALLOCATED_PRIVATE 1

// any value between 0x02 and 0xef (inclusive) means that the page is shared and the value indicates how many processes are sharing the page

// define a page base type
typedef uint64_t PAGE_BASE;

// define some pointers
static volatile PAGE_BASE *page_stack = (PAGE_BASE*)(KOFFSET + 0x1000000);				// 512k of page addresses
static TBL_PG_ALLOC *page_control = (TBL_PG_ALLOC*)(KOFFSET + 0x1400000);				// the control table

// functions
void SetupGlobalPageControl();
void MarkPageAsMapped(uint64_t page_address);
uint32_t GetFreePageCount();
void PushFreePage(uint64_t page_address);
uint64_t PopFreePage();

#endif
